//Headers
#include "sdlfuncs.h"
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include "stb_image.h"
#include <string>

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
     //Make a remporary rectangle to hold the offsets
     SDL_Rect offset;

     //Give the offsets to the rectangle
     offset.x = x;
     offset.y = y;

     //Blit the surface
     SDL_BlitSurface( source, NULL, destination, &offset );
}

/*
GLuint gl_load_image(string filename)
{
    //GL Texture
    GLuint ti;
    Texture tt;
    if(LoadTGA(&tt, filename))
    {
    }
}
*/

SDL_Surface *load_image( string filename )
{
            //Temporary storage for the image that is loaded
            SDL_Surface* loadedImage = NULL;

            //The optimized image that will be used
            SDL_Surface* optimizedImage = NULL;

            //Load the image
            loadedImage = IMG_Load( filename.c_str() );

            //If nothing went wrong in loading the image
            if( loadedImage != NULL )
            {
                //Creat and optimized image
                optimizedImage = SDL_DisplayFormat( loadedImage );

                //Free the old image
                SDL_FreeSurface( loadedImage );
            }

            //Return the optimized image
            return optimizedImage;
}

SDL_Surface *load_image_a( string filename )
{
            //Temporary storage for the image that is loaded
            SDL_Surface* loadedImage = NULL;

            //The optimized image that will be used
            SDL_Surface* optimizedImage = NULL;

            //Load the image
            loadedImage = IMG_Load( filename.c_str() );

            //If nothing went wrong in loading the image
            if( loadedImage != NULL )
            {
                //Creat and optimized image
                optimizedImage = SDL_DisplayFormat( loadedImage );

                //Free the old image
                SDL_FreeSurface( loadedImage );

                //If the image was optimized just fine
                if (optimizedImage != NULL)
                {
                    //Map the color key
                    Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0xFF, 0, 0xFF);

                    //Set all pixels of colour color key to be transparent
                    SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
                }
            }

            //Return the optimized image
            return optimizedImage;
}

bool check_collision(SDL_Rect A, SDL_Rect B)
{
    //Sides
    int leftA = A.x;
    int rightA = A.x + A.w;
    int topA = A.y;
    int botA = A.y + A.h;

    //B
    int leftB = B.x;
    int rightB = B.x + B.w;
    int topB = B.y;
    int botB = B.y + B.h;

    //Check Outside
    if( leftA > rightB )
    {
        return false;
    }
    if( rightA < leftB )
    {
        return false;
    }
    if( topA > botB )
    {
        return false;
    }
    if( botA < topB )
    {
        return false;
    }

    return true;
}

bool check_collisionabs(SDL_Rect A, SDL_Rect B[], int N_SOLIDS)
{
    //Sides
    int leftA = A.x;
    int rightA = A.x + A.w;
    int topA = A.y;
    int botA = A.y + A.h;
    //B
    int leftB;
    int rightB;
    int topB;
    int botB;

    //Flag
    bool col = true;

    for(int i = 0; i < N_SOLIDS; ++i)
    {
        col = true;
        //B
        leftB = B[i].x;
        rightB = B[i].x + B[i].w;
        topB = B[i].y;
        botB = B[i].y + B[i].h;

        //Check Outside
        if( leftA >= rightB )
        {
            col = false;
        }
        if( rightA <= leftB )
        {
            col = false;
        }
        if( topA >= botB )
        {
            col = false;
        }
        if( botA <= topB )
        {
            col = false;
        }

        if(col)
        {
            return true;
        }
    }

    return false;
}

bool check_collisionstand(SDL_Rect A, SDL_Rect B[], int N_BOXES)
{
    //Sides
    int leftA = A.x;
    int rightA = A.x + A.w;
    int botA = A.y + A.h;
    //B
    int leftB;
    int rightB;
    int topB;

    //Flag
    bool stand = true;

    for(int i = 0; i < N_BOXES; ++i)
    {
        stand = true;

        //B
        leftB = B[i].x;
        rightB = B[i].x + B[i].w;
        topB = B[i].y;

        //Check top contact
        //Make sure A is higher than B
        if( botA > topB + 2 or botA < topB - 8)
        {
            stand = false;
        }

        //Make sure the left side of A is left of the right side of B
        //OR
        //The right side of A is right of the left side of B
        if( leftA + 2 > rightB )
        {
            stand = false;
        }

        if( rightA - 2 < leftB )
        {
            stand = false;
        }

        //If it fits these criterea, A is standing on B
        if(stand)
        {
            return true;
        }
    }

    //If it is never standing on a box, return false
    return false;
}
